package www.xudan.android_ffmpeg;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;

import VideoHandle.EpEditor;
import VideoHandle.OnEditorListener;
import www.xudan.android_ffmpeg.utils.DisplayUtil;

import static www.xudan.android_ffmpeg.Constance.path;
import static www.xudan.android_ffmpeg.Constance.path_copy_video;

public class TestActivity extends AppCompatActivity implements View.OnClickListener {
    //    widthPixels = 1080,heightPixels = 1920
//    ffmpeg -y -i 原材料视频 -s 宽*高 输出的目的视频源
    private VideoView mVideoView;
    private Button mBtn;
    private boolean editing = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        Log.i("xd---", DisplayUtil.height + "---" + DisplayUtil.width);
        mBtn = findViewById(R.id.btn_test);
        mBtn.setOnClickListener(this);
        mVideoView = findViewById(R.id.videoView);

        initVideoView(mVideoView, path_copy_video);

    }

    private void initVideoView(final VideoView videoView, String path) {
        videoView.setVideoPath(path);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                videoView.start();
            }
        });
    }

    private void changeWidthAndHeight(final String inputpath, final String outpath) {
        Log.i("xd---", inputpath);
        Log.i("xd---", outpath);
        editing = true;

        File origin_file = new File(inputpath);
        if (!origin_file.exists()) {
            return;
        }
        File file = new File(outpath);
        if (file.exists()) {
            file.delete();
        } else {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
        }
//        String cmd = "-y -i " + inputpath + " -s 1080*1920 " + outpath;
        String cmd = "-i " + inputpath + " -vf pad=1280:1920:0:600:black " + outpath;//给视频不黑边
        edpt_w_h(cmd, outpath);

    }

    private void edpt_w_h(String cmd, final String outpath) {
        Log.i("xd---", cmd);
        EpEditor.execCmd(cmd, 10, new OnEditorListener() {
            @Override
            public void onSuccess() {
                Log.i("xd------", "onSuccess");
                editing = false;
                Intent intent = new Intent(TestActivity.this, ShowActivity.class);
                intent.putExtra("path", outpath);
                startActivity(intent);

            }

            @Override
            public void onFailure() {
                editing = false;
                Log.i("xd------", "onFailure");

            }

            @Override
            public void onProgress(float progress) {
                Log.i("xd------", "progress--" + progress);

            }
        });
    }

    @Override
    public void onClick(View view) {
        if (!editing) {
            changeWidthAndHeight(path_copy_video, path + "w_h.mp4");
        } else {
            Toast.makeText(this, "正在编辑...", Toast.LENGTH_SHORT).show();
        }

    }
}
